﻿using System;
using System.Collections.Generic;
using Brouteforce.gameobjs;
using Brouteforce.utilities;
using Microsoft.Xna.Framework;
using Brouteforce.Characters;
using Brouteforce.Map;

namespace Brouteforce.GameObjects {
    public class Player {
        public int PlayerId;
        public Color PlayerColor;

        public List<Character> Characters;
        public Character CurrentCharacter;
        public List<MapCell> Towers;
        public Resources CurrentResources;
        public Resources ResourcesPerTurn;



        public Player(int playerId, MapCell initialTower, Color color, Resources initRes, Resources resPerTurn, Character initCharacter) {
            PlayerId = playerId;
            PlayerColor = color;
            Characters = new List<Character>();

            CurrentResources = initRes;
            ResourcesPerTurn = resPerTurn;

            Towers.Add(initialTower);

            CurrentCharacter = initCharacter;
            Characters.Add(initCharacter);
        }




        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="tile">The tile the mouse clicked</param>
        //public void MouseClicked(Tile tile) {
        //    switch (PlayersManager.InputState) {
        //        case State.Selection:
        //            if (SelectGamePiece(tile))
        //                PlayersManager.InputState = State.Walking;
        //            break;

        //        case State.Walking:
        //            MoveGamePiece(CurrentGamePieceTile, tile, tile.IsHighlighted());
        //            PlayersManager.InputState = State.Selection;
        //            break;

        //        case State.Firing:
        //            Fire(tile);
        //            break;

        //        case State.GeneratingNewGamePiece:
        //            GenerateNewPiece(tile);
        //            PlayersManager.InputState = State.Selection;
        //            break;
        //    }
        //}

        //private void GenerateNewPiece(Tile tile) {

        //    if (tile.ContainedGamePieces.Count == 0) {
        //        GamePiece gamePiece = DashboardManager.GenerateNewCharacter(tile);
        //        gamePiece.BelongsToPlayer = PlayerId;
        //        tile.ContainedGamePieces.Add(gamePiece);
        //        AddGamePiece(gamePiece, tile);
        //    }

        //}



        public void AddTower(MapCell tower) {
            Towers.Add(tower);
            ResourcesPerTurn.AddResources(tower.Resources);
        }

        public void RemoveTower(MapCell tower) {
            Towers.Remove(tower);
            ResourcesPerTurn.RemoveResources(tower.Resources);
        }


        /// <summary>
        /// Adds a game piece to the player pieces
        /// </summary>
        /// <param name="gamePiece"></param>
        /// <param name="tile"></param>
        public void AddCharacter(Character character) {
            Characters.Add(character);
            CurrentResources.RemoveResources(character.InitialResources);
            //Currentstate = State.Walking;
        }


        //public void Fire(Tile toTile) {
        //    //foreach (GamePiece gamePiece in toTile.ContainedGamePieces) {
        //    //if (!gamePiece.BelongsToPlayer.Equals(PlayerId)
        //    //    & !gamePiece.BelongsToPlayer.Equals(GameManager.ComputerId)) {
        //    if (CurrentCharacter is Shooter)
        //        CurrentCharacter.Fire(CurrentGamePieceTile, toTile);
        //    else if (CurrentCharacter is SuicideBomber)
        //        Explode();
        //    //}
        //    //}

        //}

        //private void Explode() {
        //    CurrentCharacter.Fire(CurrentGamePieceTile, CurrentGamePieceTile);
        //}

        public void InitTurn() {
            CorrectResources();
            //foreach (var gamePiece in Characters) {
            //    gamePiece.NumberOfMovesLeft = gamePiece.Speed;
            //    gamePiece.ShotsLeft = GamePiece.ShotsPerTurn;
            //}
        }

        private void CorrectResources() {
            CurrentResources.AddResources(ResourcesPerTurn);
        }

        public void RemoveGamePiece(Character character) {
            Characters.Remove(character);
        }
    }
}